package com.sc.socket.udp;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;

/**
 * @author shuc001
 * @Date 2019-02-02 23:26
 */
public class Provider {
    public static void main(String[] args) throws IOException {
        //指定一个接口接收消息
        DatagramSocket ds = new DatagramSocket(20000);
        final  byte[] buf = new byte[512];
        DatagramPacket packet = new DatagramPacket(buf,buf.length);

        ds.receive(packet);
        String ip = packet.getAddress().getHostAddress();
        int port = packet.getPort();
        int dataLen = packet.getLength();

        String data = new String(packet.getData(),0,dataLen);
        System.out.println(ip+port+"   "+data);
        //构建会送数据
        String resp = "receive data";
        byte[] res =  resp.getBytes();
        DatagramPacket response = new DatagramPacket(res, res.length, packet.getAddress(), port);
        ds.send(response);

        ds.close();
        System.out.println("完成");
    }
}
